require 'watir'
include Watir
require 'test/unit'
require '../watir.config.rb'


class TC_Categories_test < Test::Unit::TestCase
  
  def startup
    $NewCategory = 'Category1'
    $NewCategoryEdit =$NewCategory+'Edit'
    $CategoryDescription = 'Description for the category'
    $MetaDescription = 'meta description'
    $MetaDescriptionEdit =$MetaDescription +'editing changes'
    $MetaKeyWords = 'meta key words'
    $MetaKeyWordsEdit = $MetaKeyWords + '-Edit'
    $FeedBurner = 'Feeds'
    $FeedBurner  = $FeedBurner +'Edit'
    @@ie = Watir::IE.start($SITE_URL)
    end
    
  def ie
      if defined? @@ie
        @@ie
      else
        startup
      end
    end
 
 #file1 = File.open("D:/tellitests/graffiti/Graffiti/Web/robots.txt")  
 def test_01
     ie.goto($SITE_URL+'graffiti-admin/')
     sleep 5
   end
     def test_02_verifyLoginLinksExist
      if !(ie.link(:href,$SITE_URL+'login/').exists?)
        ie.goto($SITE_URL + 'graffiti-admin/logout')
        ie.refresh
        ie.goto($SITE_URL)
        ie.refresh
      end
	  end
    def test_03_login
        ie.goto($SITE_URL+'login/')
        assert(ie.text_field(:id,'UserName').exists?)
        ie.text_field(:id,'UserName').value = $ADMIN_USER
        assert(ie.text_field(:id,'Password').exists?)
        ie.text_field(:id,'Password').value = $ADMIN_PASSWORD
        ie.button(:id,'login').click
        ie.goto($SITE_URL+'graffiti-admin/')
      end
      
   def test_04_addCategory     
        ie.link(:text,'Categories').click
        sleep 3
        assert(ie.text_field(:id,/txtCategory$/).exists?)
        sleep 2
        ie.text_field(:id,/txtCategory$/).set($NewCategory)
        assert(ie.select_list(:id,/Categories$/).exists?)
        sleep 3
        ie.select_list(:id,/Categories$/).select('[No parent category]')
        sleep 2
        assert(ie.button(:id,/CreateCategory$/).exists?)
         ie.button(:id,/CreateCategory$/).click
       end 
    def test_04_addDetails
        sleep 3
        #ie.ie.Document.ParentWindow.execscript("tinyMCE.get('ctl00_MainRegion_Editor').execCommand('mceSetContent', false,'"+$CategoryDescription+"')")
        ie.table(:id,/SortOrder$/).row(:index,2).radio(:id,/SortOrder_1$/).set
        sleep 3
        assert(ie.text_field(:id,/txtMetaScription$/).exists?)
        sleep 3
        ie.text_field(:id,/txtMetaScription$/).value = $MetaDescription
        assert(ie.text_field(:id,/txtKeywords$/).exists?)
        sleep 3
        ie.text_field(:id,/txtKeywords$/).value = $MetaKeyWords
        assert(ie.text_field(:id,/txtFeedBurner$/).exists?)
        sleep 3
        ie.text_field(:id,/txtFeedBurner$/).value = $FeedBurner
        sleep 2
        assert(ie.button(:id,/EditCategory$/).exists?)
        ie.button(:id,/EditCategory$/).click
      end
    def test_05_editCategory
       assert(ie.text_field(:id,/txtCategory$/).exists?)
        sleep 2
        ie.text_field(:id,/txtCategory$/).set($NewCategoryEdit)
        assert(ie.select_list(:id,/Categories$/).exists?)
        sleep 3
        ie.select_list(:id,/Categories$/).select('[No parent category]')
        sleep 2
        ie.table(:id,/SortOrder$/).row(:index,2).radio(:id,/SortOrder_1$/).set
        sleep 3
        assert(ie.text_field(:id,/txtMetaScription$/).exists?)
        sleep 3
        ie.text_field(:id,/txtMetaScription$/).value = $MetaDescriptionEdit
        assert(ie.text_field(:id,/txtKeywords$/).exists?)
        sleep 3
        ie.text_field(:id,/txtKeywords$/).value = $MetaKeyWordsEdit
        assert(ie.text_field(:id,/txtFeedBurner$/).exists?)
        sleep 3
        ie.text_field(:id,/txtFeedBurner$/).value = $FeedBurnerEdit
        sleep 2
        assert(ie.button(:id,/EditCategory$/).exists?)
        ie.button(:id,/EditCategory$/).click
      end
      def test_06_deleteCategory
         ie.link(:text,'Categories').click
        sleep 5 
        ie.ul(:class, 'listboxes').li(:index,3).link(:text,$NewCategoryEdit).click
      end
      
end